import { cloneDeep } from 'lodash-es'

export function useTable() {
    const gridOptions = reactive<any>({
        border: true,
        columnConfig: {
            resizable: true,
        },
        columns: [
            { title: '', type: 'checkbox', width: '60' },
            { field: 'huxmc', title: '户型名称' },
            { field: 'xiangmmc', title: '所属项目' },
            { field: 'shangjspzt', slots: { default: 'status' }, title: '上架审批状态' },
            { field: 'suosqy', title: '所属运营企业' },
            { field: 'xingzqh', title: '行政区划' },
            { field: 'xiangmdz', title: '项目地址' },
            { field: 'CreatedName', title: '创建人' },
            { field: 'CreatedTime', title: '创建时间' },
            { slots: { default: 'operate' }, title: '操作', width: 300 },
        ],
        data: [],
        editConfig: {
            mode: 'row',
            showStatus: true,
            trigger: 'manual',
        },
        height: 530,
        keepSource: true,
        loading: false,
        pagerConfig: {
            currentPage: 1,
            enabled: true,
            pageSize: 10,
            pageSizes: [10, 20, 50, 100, 200, 500],
            total: 0,
        },
        showOverflow: true,
    })

    const btns = [
        {
            label: '户型详情',
            type: 'primary',
            value: 'houseDetails',
        },
        {
            label: '审批详情',
            value: 'details',
        },
        {
            label: '审批',
            value: 'pass',
        },
        {
            label: '驳回',
            value: 'refuse',
        },
    ]
    const formatTag = (row: any) => {
        if (row?.memberTagList && row?.memberTagList?.length)
            return row?.memberTagList

        return []
    }
    // 删除空属性
    const paramsIsNull = (obj: any) => {
        const _newPar = {}
        for (const key in obj) {
            if (
                (obj[key] === 0 || obj[key])
                && obj[key].toString().replace(/(^\s*)|(\s*$)/g, '') !== ''
            )
                _newPar[key] = obj[key]
        }
        // 返回新对象
        return _newPar
    }

    // TODO:多条件查询方法 仅字符串对比
    const useQuery = (data: any, params: any) => {
        const notNullParams = paramsIsNull(params)
        return data.filter((item) => {
            return Object.keys(notNullParams).every((key) => {
                return (
                    item[key] !== undefined
                    && item[key].includes(notNullParams[key])
                )
            })
        })
    }
    const useFilter = (form: any) => {
        const arr: any = []
        for (const key in form) {
            if (Object.prototype.hasOwnProperty.call(form, key)) {
                if (form[key] !== undefined && form[key] !== null && form[key] !== '') {
                    arr.push({
                        filed: key,
                        value: form[key],
                    })
                }
            }
        }
        return arr
    }
    // 前端分页
    const usePage = (data: any, gridOptions: any) => {
        const { currentPage, pageSize } = gridOptions.pagerConfig
        const val = cloneDeep(data)
        const list = val.splice((currentPage - 1) * pageSize, pageSize)
        return {
            list,
            total: data.length,
        }
    }

    return { btns, formatTag, gridOptions, useFilter, usePage, useQuery }
}
